package com.etlmaster.executor.scheduler.utils;

import java.util.ArrayList;
import java.util.List;

import com.etlmaster.executor.utils.DataCollectionUtils;
import com.etlmaster.executor.utils.LogSqlEntry;
import com.etlmaster.executor.utils.LogWriter;
import com.etlmaster.jdbc.JdbcTemplate;

/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
public class LogClusterInfoUtil {
	private static List<LogSqlEntry> sqls = new ArrayList<LogSqlEntry>();

	public static void insertNewHost(String hostId, String host, String role,
			int status) {
		String[] args = { host, role, hostId, status+"" };
		String newHostSql = DataCollectionUtils.getProperties("newHostSql");
		LogSqlEntry entry = new LogSqlEntry(newHostSql,  args);
		String deletesql=DataCollectionUtils.getProperties("deleteHostSql");
		String[] deleteargs = { hostId};
		LogSqlEntry deletentry = new LogSqlEntry(deletesql,  deleteargs);

		sqls.add(deletentry);

		sqls.add(entry);
	}

	public static void updateHostStatus(String hostId, int status) {
		String[] args = { status+"", hostId };
		String updateHostSql = DataCollectionUtils
				.getProperties("updateHostStatus");
		LogSqlEntry entry = new LogSqlEntry(updateHostSql,  args);
		sqls.add(entry);
	}

	public static void batchUpdate() {

		try {
			JdbcTemplate jdbc = new JdbcTemplate();

			for (LogSqlEntry entry : sqls) {
				jdbc.update(entry.getSql(), entry.getArgs());
			}

		} catch (Exception e) {
			LogWriter.addLog("ERROR", "更新集群信息失败，{}", e.getMessage());
		}

		sqls.clear();
	}
}
